<?
 Prado::using('System.Web.UI.ActiveControls.*'); 
 Prado::using('System.Web.TAssetManager.*'); 
 Prado::using('Application.pages.utils.CommonUtils');
 
class FriendsUser extends TPage
{
	public $found = "true";
	//public $serverPathUpload = "d:\\myWeb\\smartbook\\assets\\images\\";
	//public $serverPathDownload = "assets/images/";
	public $id = -1;
	// hi
	public function onInit($param)
    {	
//			parent::onInit($param);
			if(!$this->IsPostBack)  // if the page is requested the first time
			{
			}
			$this->id = $this->Request['id'];
			$this->setTitle($this->getCurrentRecord()->username ."'s Friends");
			// friend Repeater
			$f_1 = FriendRecord::finder()->count('user_id = ?',$this->id);
			$f_2 = FriendRecord::finder()->count('friend_id = ?',$this->id);
			$this->numFriend->Text = "(". ($f_1 + $f_2) . ")";
			$this->Repeater_friend->VirtualItemCount = $f_1 + $f_2;
			
			$this->populateData_friend();
	}
	
	public function buttonClicked($sender, $param)
    {
		//echo "buttonClicked " .$this->user->ID." /s/" ;
        $f_email = $this->txtSendFriendReq->Text;
		//echo "buttonClicked ".$this->owner->user_id;
		//$this->reqFriend($f_email);	
		$this->onInit($param); // call this onInit to refresh the page
		
    }
    
    public function reqFriend($f_email)
	{
		if($f_email == $this->user->Email)
		{
			$this->result->Text = "You cannot request yourself as a friend.";
			return;
		}
		
		$finder = UserRecord::finder()->findByEmail($f_email);
		
		if($finder->first_name == "")
			$this->result->Text = "\"". $f_email ."\" is not found in our database.";
		else
		{
			if($this->isInverseRequest($finder))
			{
				$this->result->Text = "\"" .$finder->username . "\" already invited you as friends";
				return;
			}
			if($this->isAlreadyFriend($finder))
			{
				$this->result->Text = "\"" .$finder->username . "\" is your friends already";
				return;
			}
			// OK
				$this->result->Text = "You have requested \"" .$finder->username . "\" as a friend";
			try
			{
				$this->insertRecord($finder);
			}
			catch(TDbException $e)
			{
				$this->result->Text = "You cannot request more than one time.";
				//$this->onError($e);
			}
			
		}
	}
    public function insertRecord($finder)
	{
		$f_id = $finder->user_id;
		//$owner = UserRecord::finder()->findByEmail($this->user->Email);
		
		$u_id = $this->User->ID;
		$newRec = new FriendReqRecord();
		$newRec->user_id = $u_id;
		$newRec->friend_id = $f_id;
		$newRec->datetime_request = date("Y/m/d H:i:s");
		$newRec->save();
	}
    public function isInverseRequest($finder)
	{
		$f_id = $finder->user_id;
	//	$owner = UserRecord::finder()->findByEmail($this->user->Email);
		$u_id = $this->id;
		// inverse checking ...
		$count = FriendReqRecord::finder()->count('user_id = ? AND friend_id = ?',$f_id ,$u_id);
		if($count == 1)
			return true;
		else 
			return false;
	}
    
	public function isAlreadyFriend($finder)
	{
		$f_id = $finder->user_id;
		//$owner = UserRecord::finder()->findByEmail($this->user->Email);
		$u_id = $this->id;
		// inverse checking ...
		$count = FriendRecord::finder()->
		count(' (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)',$f_id ,$u_id,$u_id ,$f_id);
		if($count == 1)
			return true;
		else 
			return false;
	}
	
	protected function populateData_friend()
    {
        $offset=$this->Repeater_friend->CurrentPageIndex*$this->Repeater_friend->PageSize;
        $limit=$this->Repeater_friend->PageSize;
        if($offset+$limit > $this->Repeater_friend->VirtualItemCount)
            $limit=$this->Repeater_friend->VirtualItemCount-$offset;
        $this->Repeater_friend->DataSource=$this->getRequests_friend($offset,$limit);
        $this->Repeater_friend->dataBind();
	//	echo $this->Repeater->VirtualItemCount;
    }
	
	protected function getRequests_friend($offset, $limit)
    {
        // Construts a query criteria
        $criteria=new TActiveRecordCriteria;
		//$owner = UserRecord::finder()->findByEmail($this->user->Email);
		
		$criteria->Condition = 'friend_id = :id OR user_id = :id';
		$criteria->Parameters[':id'] = $this->id;
        
		
        $criteria->Limit=$limit;
        $criteria->Offset=$offset;
	//	echo $criteria;
		//return;
        // query for the posts with the above criteria and with author information
        return FriendRecord::finder()->findAll($criteria);
    }
	
	public function pageChanged_friend($sender,$param)
    {
        // change the current page index to the new one
        $this->Repeater_friend->CurrentPageIndex=$param->NewPageIndex;
        // re-populate data into the repeater
        $this->populateData_friend();
    }
	
	public function getCurrentRecord()
	{
	
	//	$userRecord = UserRecord::finder()->findByEmail($this->user->Email); $this->id
		$userRecord = UserRecord::finder()->findByUser_Id($this->id);
		return $userRecord;
	}
	
	public function getAvatar($user)
	{
		$img_path = $this->Application->Parameters['ImageFolderName'];
		return CommonUtils::getAvatar($img_path,$user);	
	}
	
}